import { getProvinceApi, getWarehouseCodeByCountryIdApi, getCoumsterChannelByCountryApi } from "@/api/common";

import { reactive, ref } from "vue";
import { getListApi } from "@/api/configCenter/countryRegion";
import { getListApi as getMasterialApi } from "@/api/configCenter/material";
import CommonHook from "@/hook/commonHook";
export default function InitHook() {
  const { getClassifyArrayByFields, dataDict } = CommonHook();
  const formState = reactive<Partial<ClientAddressManagement.AddPickupAddressForm>>({
    countryRegionId: undefined,
    stateCode: undefined,
    state: undefined,
    cityCode: undefined,
    city: undefined,
    zoneCode: undefined,
    zone: undefined,
    streetCode: undefined,
    street: undefined,
    address: undefined,
    contact1: undefined,
    phone1: undefined,
    contact2: undefined,
    phone2: undefined,
    warehouseCode: undefined,
    cityModel: []
  });
  const loading = ref<boolean>(false);
  const formDataRef = ref<any>(null);
  const areaOpts = ref<Array<Base.GeneraDataType>>([]);
  const streetOpts = ref<Array<Base.GeneraDataType>>([]);
  const warehouseCodeOpts = ref<Array<Base.GeneraDataType>>([]);
  const getAreaOpts = () => {
    return getProvinceApi().then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        areaOpts.value = data;
      }
    });
  };
  const countryOpts = ref<Array<Base.GeneraDataType>>([]);
  const getAllCountryOpts = () => {
    return getListApi({ pageNum: 1, pageSize: 999999, status: 1 }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        countryOpts.value = data.records || [];
      }
    });
  };
  const channelOpts = ref<Array<Base.GeneraDataType>>([]);
  const getChannelOpts = () => {
    const customerId = JSON.parse(sessionStorage.getItem("user") as any).id;
    return getCoumsterChannelByCountryApi({
      customerId,
      countryRegionId: formState.countryRegionId,
      warehouseCode: formState.warehouseCode[1]
    }).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        channelOpts.value = getClassifyArrayByFields(data || [], "channelAllocation", "disable");
      }
    });
  };
  const materialOpts = ref<Array<MaterialIndex.ListRows>>([]);
  const getAllMaterialOpts = () => {
    return getMasterialApi({ pageNum: 1, pageSize: 999999, isShow: 1, status: 1 } as MaterialIndex.SearchForm).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        materialOpts.value = data.records || [];
      }
    });
  };
  const getAllWarehouseCodeOpts = () => {
    return getWarehouseCodeByCountryIdApi(formState.countryRegionId).then((res: any) => {
      const { code, data } = res;
      if (code === 200) {
        warehouseCodeOpts.value =
          data.map((t: any) => {
            return {
              ...t,
              warehouseCode: t.warehouseTypeName
            };
          }) || [];
      }
    });
  };
  return {
    formState,
    formDataRef,
    loading,
    getAreaOpts,
    areaOpts,
    streetOpts,
    getAllCountryOpts,
    countryOpts,
    channelOpts,
    getChannelOpts,
    getAllMaterialOpts,
    materialOpts,
    warehouseCodeOpts,
    getAllWarehouseCodeOpts,
    dataDict
  };
}
